package com.lik888.web1.Aspect;


import com.lik888.web1.constants.UserContext;
import com.lik888.web1.entity.LogTable;
import com.lik888.web1.mapper.GoodsTableMapper;
import com.lik888.web1.mapper.LogTableMapper;
import lombok.RequiredArgsConstructor;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import static com.lik888.web1.constants.AOPContext.Look_EXPRESSION;

@Component
@Aspect
@RequiredArgsConstructor
@Order(2)
public class LookAspectJ {
   @Autowired
    private final GoodsTableMapper goodsTableMapper;
    private final LogTableMapper logTableMapper;

    @Pointcut(Look_EXPRESSION)
    public void logPointcut() {

    }
    @Around(Look_EXPRESSION)
    //切换成环绕通知
    public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {

        System.out.println("========== 【开始通知啦！】 ==========");
        //开启新的
        //1.日志记录
        //获取方法名
        String methodName = joinPoint.getSignature().getName();
        Long LogUser = UserContext.getUser();
        //获取方法参数
        //执行目标方法
        Object returnVal = joinPoint.proceed();
        Object[] args = joinPoint.getArgs();
        //获取参数第一个参数
        Object firstParam = args[0];
        // GoodsID
        Long GoodsID = (Long) firstParam;
       //修改GoodsTable 将Looking + 1
        goodsTableMapper.updateLook(GoodsID);
        //TODO 记录日志到数据库
        LogTable logTable = new LogTable();
        logTable.setLogUser(LogUser);
        logTable.setLogMethodname(methodName);
        logTable.setLogPart("商品查看量为：" +goodsTableMapper.selectById(GoodsID).getGoodsLook().toString());
        logTable.setLogUser(LogUser);
        logTableMapper.insert(logTable);
      System.out.println("========== 【Aspectj后置通知】 ==========");
        return returnVal;
    }


}
